package cn.hanyx.coder.design.pattern.creational.builder;

/**
 * ConstructorArg
 *
 * @author hanyx
 * @date 2020/05/15
 */
public class ConstructorArg {
    private boolean isRef;
    private Class type;
    private Object arg;

    private ConstructorArg(Builder builder) {
        this.isRef = builder.isRef;
        this.type = builder.type;
        this.arg = builder.arg;
    }

    public boolean isRef() {
        return isRef;
    }

    public Class getType() {
        return type;
    }

    public Object getArg() {
        return arg;
    }

    @Override
    public String toString() {
        return "ConstructorArg{" +
                "isRef=" + isRef +
                ", type=" + type +
                ", arg=" + arg +
                '}';
    }

    public static class Builder {
        private boolean isRef;
        private Class type;
        private Object arg;

        public Builder setRef(boolean ref) {
            isRef = ref;
            return this;
        }

        public Builder setType(Class type) {
            this.type = type;
            return this;
        }

        public Builder setArg(Object arg) {
            this.arg = arg;
            return this;
        }

        public ConstructorArg build() {
            if (this.isRef) {
                this.arg = ConstructorArg.class.getSimpleName();
                this.type = null;
            } else {
                if (this.type == null) {
                    throw new RuntimeException("type should not be empty.");
                }
                if (this.arg == null) {
                    throw new RuntimeException("arg should not be empty.");
                }
            }
            return new ConstructorArg(this);
        }
    }

    public static void main(String[] args) {
        ConstructorArg constructorArg = new ConstructorArg.Builder().setRef(false).build();
        System.out.println(constructorArg);
    }
}
